function userLogin(){	
	var self=this;
	var username=$('username');
	var loginBtn=$('loginBtn');
	var passwd=$('passwd');
	username.focus();
	this.init=function(){
		var initUsername=cookies.get('budgetUsername');
		var initPwd=cookies.get('budgetPasswd');
		if(false==initUsername||false==initPwd)	return;
		username.value=initUsername;
		passwd.value=initPwd;
	}
	this.pressKey=function(e){
		if(13==e.keyCode)
			if(self.checkInput())	self.login();
	}
	this.checkInput=function(){
		if(''==username.value){
			alert('请输入用户名！');
			username.focus();
			return false;
		}
		if(''==passwd.value){
			alert('请输入密码！');
			passwd.focus();
			return false;
		}
		return true;
	}
	this.login=function(){
		if($('saveCookie').checked){
			cookies.set('budgetUsername',username.value,60);//60天
			cookies.set('budgetPasswd',passwd.value,60);
		}
		ajax.post('utils/user.php',postParam('auth',{'username':username.value,'passwd':passwd.value}),function(re){
			authResult(re);
 		});
	}
	this.init();
	eve.observe(loginBtn,'mouseover',function(){this.className='btn btn_over'});
	eve.observe(loginBtn,'mouseout',function(){this.className='btn'});
	eve.observe(loginBtn,'click',function(){if(self.checkInput())	self.login();});
	eve.observe(username,'keypress',this.pressKey);
	eve.observe(passwd,'keypress',this.pressKey);
	eve.observe($('visitorLogin'),'click',function(){//游客登录
		ajax.post('utils/user.php',postParam('visitorLogin',{}),function(re){
			authResult(re);
		});
	});
}
function authResult(re){
	var result=parseJson(re);
	if(0==result.result)
		alert(result.reason);
	else{
 		SYS_STATUS.userInfo=result;
		loadMainInterface();
	}
}
function autoLogin(userid){
	ajax.post('utils/user.php',postParam('getUserDetail',{'userid':userid}),function(re){
		authResult(re);
	});
}
function unusable(){
	alert('Sorry,This function is unusable now.对不起,该功能现在不可用.');
}

function loadMainInterface(){
	$('loginDiv').className='hide';//隐藏登录窗口
	$('workDiv').className='show';//显示主窗口
	SYS_STATUS.loading();//显示加载条
	//psbirdDialog.info('Oil Test and Well Workover Budget System is a real project,which is still being developed by me,now everything you see is a demo.<br/><br/>这套试油修井定额系统是一个真实的项目,我还在开发中,现在各位看到的是一个演示<br/><br/>',400);
	var menuList=$('menuList');
	menuList.appendChild($C('div',{'class':'userinfo'},'<span class="red">'+SYS_STATUS.userInfo.name+'</span>: <span class="black">'+fixRole[SYS_STATUS.userInfo.role]+'</span>'));
	var menus=SYS_STATUS.userInfo.menu;
	for(var i=0;i<menus.length;i++){
		menuList.appendChild(makeMenuItem(menus[i].css,menus[i].func,menus[i].menuName));
	}
	showMessage();//加载最新消息
	setInterval(showMessage,3000*60);//每3秒钟刷新一次信息
	//SYS_STATUS.currentMenu=helpMenu;//当前使用的menu项
	SYS_STATUS.editPanel=$('editPanel');//编辑面板
	SYS_STATUS.isEditing=false;//是否正处于编辑状态
	SYS_STATUS.isDblClick=false;//是否是双击
	SYS_STATUS.menuFibidden=false;//禁止点击菜单。防止点击过快而出错
	SYS_STATUS.isTextarea=false;//是否处于textarea编辑框
	eve.observe(SYS_STATUS.editPanel,'dblclick',goToEditCell);
	eve.observe(document,'keydown',inputValue);
	eve.observe(window,'unload',function(){
		if(false==isNeedSave())	return;//如果不需要保存,则返回
		var pack=makeSavePake();
		if(false==confirm("文件已经被修改,是否保存修改?"))	return;postBudget(pack);
	});
	ajax.get('utils/template.php?action=list',function(result){//把所有模板的名称的目录取回本地
		var re=parseJson(result);
		for(var i=0;i<re.length;i++){
			TEMPLATES[re[i]['shortname']]=re[i]['name'];
		}
		SYS_STATUS.loadComplete();//关闭加载条
	});
	setInterval(function(){
		ajax.post('utils/user.php',postParam('getUserDetail',{'userid':SYS_STATUS.userInfo.userid}),emptyFunc);
	},5*60*1000);//每5分钟发一次请求，如果session已经过期，则延长session时间
}
function makeMenuItem(cName,actionEvent,word){
	var aMenu=$C('div',{'class':cName},word);
	aMenu.callBack=actionEvent;
	eve.observe(aMenu,'click',clickMenu);
	return aMenu;
}
function help(){
	window.open('html/help.html');
}
function clickMenu(e){
	switchMenu(e.target||e.srcElement);
}
function switchMenu(clickMenu){
	this.doSwitch=function(){
		addClass(clickMenu,'using');
		if(null!=SYS_STATUS.currentMenu)	removeClass(SYS_STATUS.currentMenu,'using');
		SYS_STATUS.currentMenu=clickMenu;
		eval(clickMenu.callBack)();
		SYS_STATUS.menuFibidden=true;
		setTimeout(function(){SYS_STATUS.menuFibidden=false;},1000);//1秒之内不能再点击菜单
	}
	if(true==SYS_STATUS.menuFibidden)	return;//防止过于频繁点击菜单
	if(clickMenu==SYS_STATUS.currentMenu)	return;//点击的是当前正在使用的菜单
	if(false!=SYS_STATUS.selectCell)	closeEditPanel();//确保输入框处于关闭状态
	$('quotaTree').style.display='none';//确保quotaTree处于关闭状态
	if(true==isNeedSave())
		askIsSave(this.doSwitch);
	else
		this.doSwitch();
}

function newFile(){
	var self=this;
	this.nextStep=function(){
		var wellName=$('wellName').value.trim();
		if(''==wellName)	return	alert('井号不能为空');
		self.okBtn.value='请等待..';
		ajax.post('utils/file.php',postParam('newFile',{'wellno':wellName,'tempno':$('templist').value}),function(result){
			var re=parseJson(result);
			if('0'==re.result){
				self.okBtn.value='下一步';
				return alert(re.reason);
			}
			eve.stopObserve(self.okBtn,'click',self.nextStep);
			(displayBudget.bind(re.fileinfo))();
		});	
	}
	this.build=function(){
		cleanContent($('workTd'));
		selObj=$C('select',{'id':'templist'});
		for(var t in TEMPLATES){
			selObj.options[selObj.options.length]=new Option(TEMPLATES[t],t);
		}
		self.okBtn=$C('input',{'value':' 下一步 ','type':'button','class':'btn btn_over'});//由于IE浏览器某些功能很奇怪，所以代码也很奇怪。
		var tmp=$C('div',{'id':'newbudget'},$C('fieldset',{},$C('legend',{},'新建概算'),$C('div',{'class':'whitebg'},'<br/><table width="100%" border="0"></table><br/>')));
		eve.observe(self.okBtn,'click',self.nextStep);
		$('workTd').appendChild(tmp);
		var t=tmp.getElementsByTagName('table')[0];
		if(true==browser.ie)//为了照顾IE浏览器，只能这样
			tmp.firstChild.className='ieField';
		else
			tmp.firstChild.style.background='#fff';
		t.appendChild($C('tbody',{},$C('tr',{},$C('td',{'width':'35%','class':'right'},'请输入井号：'),$C('td',{},$C('input',{'type':'text','size':'30','id':'wellName'}))),$C('tr',{},$C('td',{'class':'right'},'请选择计算模板：'),$C('td',{'class':'left'},selObj)),$C('tr',{},$C('td',{'colspan':2,'class':'cen'},self.okBtn))))
		
	}
	self.build();
}
function displayBudget(){//这里的this是指 fileInfo
	//alert(this);
	(new openFile(this)).init();
}
function getList(page){
	if(!page)	page=1;//如果不指定是哪一页，就显示第一页
	ajax.post('utils/file.php',postParam('listbudget',{'page':parseInt(page)}),showList);
}
function showList(re){
	(new listBudget).init(parseJson(re));
}
function manageQuota(){
	(new tree).init();
}
function manageUser(){
	ajax.post('utils/user.php',postParam('listuser',{}),function(re){
		(new listUser).init(parseJson(re));
	});
}
function modifypwd(){
	var mForm=$C('ul',{});
	mForm.appendChild($C('li',{'class':'l'},'当前密码'));
	mForm.appendChild($C('li',{'class':'r'},$C('input',{'type':'password','name':'oldpwd'})));
	mForm.appendChild($C('li',{'class':'l'},'新密码'));
	mForm.appendChild($C('li',{'class':'r'},$C('input',{'type':'password','name':'newpwd'})));
	mForm.appendChild($C('li',{'class':'l'},'密码确认'));
	mForm.appendChild($C('li',{'class':'r'},$C('input',{'type':'password','name':'repwd'})));
	var okBtn=$C('input',{'type':'button','class':'btn','value':'提交'});
	eve.observe(okBtn,'click',function(){
		var data={};
		var input=mForm.getElementsByTagName('input');
		for(var i=0;i<input.length;i++){
			data[input[i].name]=input[i].value;
		}
		if(''==data.oldpwd)	return alert('请输入当前密码');
		if(data.newpwd.length<5)	return alert('密码长度必须大于5位');
		if(data.newpwd!=data.repwd)	return alert('两次输入的新密码不一致');
		ajax.post('utils/user.php',postParam('modifypwd',{'userid':SYS_STATUS.userInfo.userid,'oldpwd':data.oldpwd,'newpwd':data.newpwd}),function(result){
			var re=parseJson(result);
			if(0==re.result)	return alert(re.reason);
			psbirdDialog.success('密码修改成功。');
			w();//关闭修改密码窗口
		});
	});
	var closeBtn=$C('input',{'type':'button','class':'btn','value':'取消'});
	var w=psbirdDialog.html($C('div',{},$C('div',{'class':'modifyForm'},mForm,$C('div',{'class':'clear'})),$C('div',{'class':'cen'},okBtn,closeBtn)),400,'修改口令');
	eve.observe(closeBtn,'click',w);
}
function about(){
	psbirdDialog.info('<br/><p><b>四川石油管理局试修工程概算系统</b></p>版本2.0<br/><br/>版权所有：四川石油管理局、黄顺钊 2007-2008');
}
function logout(){
	//return unusable();
	ajax.post('utils/user.php',postParam('logout',{}),emptyFunc);
	window.location.href='index.php';
}
function manageTemplate(){
	ajax.post('utils/template.php?action=list',postParam('list',{}),function(re){
		(new listTemplate).init(parseJson(re));
	});
}
function showMessage(){
	cleanContent($('messageList'));
	(new parseRss('db/message.xml')).init();
}
var cookies={
	set:function(name,value,expireDay){
		var expireDate;
		expireDay=expireDay||30;
　　	expireDate=new Date();
　　	expireDate.setDate(expireDate.getDate()+expireDay); 
        document.cookie=[name,'=',encodeURIComponent(value),';expires='+expireDate.toGMTString()].join('');
	},
	get:function(name){
		var cookieValue=false;
		var cookie='';
		if(document.cookie&&document.cookie!=''){
			var cks=document.cookie.split(';');
			for(var i=0;i<cks.length;i++){
				cookie=cks[i].trim();
                if (cookie.substring(0,name.length+1)==(name+'=')){
                    cookieValue=decodeURIComponent(cookie.substring(name.length+1));
                    break;
                }
			}
		}
		return cookieValue;
	}
};